//
//  ApiExt.swift
//  WNLockDemo
//
//  Created by 裴威 on 01/09/2018.
//  Copyright © 2018 wunu. All rights reserved.
//

import Foundation
import Moya

extension FxTargetType {
    public var headers: [String : String]? {
        var params: [String: String] = [:]
        if let token = UserDefaults.standard.getToken() {
            params["x-wx-skey"] = token
        }
        return params
    }
    public var baseURL: URL {
        return URL(string: "https://y8lbxvm2.qcloud.la")!
    }
    public var useApiFixture: Bool {
        return false
    }
    public var sampleData: Data {
        return Data()
    }
    public var responseHandler: FxResponseHandler {
        return { json in
            let code = json["data"]["code"].intValue
            if code == 200 {
                return .success(json["data"]["data"])
            } else {
                return .failure(NSError(domain: json["data"]["msg"].stringValue, code: json["data"]["code"].intValue, userInfo: nil))
            }
        }
    }
    public var requestPreviousHandler: FxRequestPreviousHandler {
        return { status in
            
        }
    }
    var requestEndHandler: (Moya.Response?) -> Void {
        return { resp in
            
        }
    }
}

extension Error {
    var errMsg: String {
        return (self as NSError).domain
    }
    var errCode: Int {
        return (self as NSError).code
    }
}
